﻿using System;
using Microsoft.SharePoint.Administration;
using System.Runtime.InteropServices;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
using System.Configuration;
using System.ServiceModel;
using System.ServiceModel.Configuration;

namespace CodeCounsel.SharePoint.NavigationService
{
    [GuidAttribute("0c1a4b04-90d5-4cef-be3a-f4591b1ac873")]
    public class NavigationApplicationProxy
        : SPServiceApplicationProxy
    {
        [Persisted]
        Guid _serviceApplicationID;

        public override string TypeName
        {
            get { return "Navigation Application Proxy (Demo)"; }
        }

        public override string DisplayName
        {
            get { return "Global Navigation (Demo)"; }
        }

        public NavigationApplicationProxy()
        {
        }

        public NavigationApplicationProxy(string name, NavigationApplication application, NavigationServiceProxy serviceProxy)
            : base(name, serviceProxy)
        {
            _serviceApplicationID = application.Id;
        }
        
        public static NavigationApplicationProxy Create(string name, NavigationApplication application,
            NavigationServiceProxy applicationProxy, bool addToDefaultGroup)
        {
            NavigationApplicationProxy proxy = new NavigationApplicationProxy(name, application, applicationProxy);
            proxy.Update();
            if (addToDefaultGroup)
            {
                SPServiceApplicationProxyGroup group = SPServiceApplicationProxyGroup.Default;
                group.Add(proxy);
                group.Update();
            }
            return proxy;
        }

        public static NavigationApplicationProxy GetProxy(SPServiceContext context)
        {
            return (NavigationApplicationProxy)context.GetDefaultProxy(typeof(NavigationApplicationProxy));
        }

        public string GetNavigationInformation()
        {
            NavigationApplication application = GetNavigationApplication();
            return application.GetNavigationInformation();
        }

        NavigationApplication GetNavigationApplication()
        {
            return (NavigationApplication)Farm.GetObject(_serviceApplicationID);
        }
    }
}
